<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of contest_db
 *
 * @author phamhphuc
 */
class contest_db {
    //put your code here
    private $_connection = null;
    
    public function __construct($config) {
        $con = mysql_connect($config['host'],$config['user'],$config['password']); 
        mysql_set_charset("utf8", $con);
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        } else {
            $this->_connection = $con;
            if(!mysql_select_db($config['db'], $this->_connection))
            {
                die("Can't find database {$config['db']}");
            }
        }
        
    }
    
    public function getRow($sql, $isReturnObject = false) {
        $result = mysql_query($sql);
        if($result) {
            if($isReturnObject) {
                return mysql_fetch_object($result);
            }
            return mysql_fetch_assoc($result);
        } else {
            return false;
        }        
    }
    
    public function getRows($sql, $isReturnObject = false) {
        $result = mysql_query($sql);
        $data = array();
        if($result) {
            if($isReturnObject) {
                while($row = mysql_fetch_object($result)){
                    $data[] = $row;
                }
            } else {
                while($row = mysql_fetch_assoc($result)){
                    $data[] = $row;
                }
            }
            
            return $data;
        } else {
            return false;
        }  
    }
    
    public function update($tableName, $data, $where) {
        $signal = "";
        $updateString = "";
        foreach($data as $k => $v) {
            $v1 = mysql_escape_string($v);
            $updateString .= "{$signal}{$k} = '{$v1}'";
            $signal = ",";
        }
        $sql = "update {$tableName} set {$updateString} where {$where}";
        $result = mysql_query($sql);
        if($result) {
            return true;
        } else {
            return false;
        }
    }
    
    public function insert($tableName, $data) {
        $signal = "";
        $keyString = "";
        $updateString = "";
        foreach($data as $k => $v) {
            $v1 = mysql_escape_string($v);
            $keyString .= "{$signal}`{$k}`";
            $updateString .= "{$signal}'{$v1}' ";
            $signal = ",";
        }               
        
        $sql = "insert into {$tableName} ({$keyString}) values ({$updateString})"; 
//        echo $sql;die;
        $result = mysql_query($sql);
        if($result) {
            return mysql_insert_id($this->_connection);
        } else {
            return false;
        }     
    }
    
    public function delete($tableName, $where) {
        $sql = "delete from {$tableName} where {$where}";
        return mysql_query($sql);
    }
    
    public function query($sql) {
        return mysql_query($sql);
    }
    
    public function __destruct() {
        mysql_close($this->_connection);
    }
}

?>
